<template>
  <div class="base-data-manage">
    <el-tabs v-model="activeTab">
      <!-- 活动分类管理 -->
      <el-tab-pane label="活动分类管理" name="category">
        <div class="operation-bar">
          <el-button type="primary" @click="handleAdd('category')">新增分类</el-button>
        </div>
        <el-table :data="categories" style="width: 100%">
          <el-table-column prop="name" label="分类名称" />
          <el-table-column prop="status" label="状态">
            <template #default="scope">
              <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
                {{ scope.row.status === 1 ? '启用' : '禁用' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template #default="scope">
              <el-button type="primary" @click="handleEdit('category', scope.row)">编辑</el-button>
              <el-button type="danger" @click="handleDelete('category', scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>

      <!-- 教学班类型管理 -->
      <el-tab-pane label="教学班类型管理" name="classType">
        <div class="operation-bar">
          <el-button type="primary" @click="handleAdd('classType')">新增类型</el-button>
        </div>
        <el-table :data="classTypes" style="width: 100%">
          <el-table-column prop="name" label="类型名称" />
          <el-table-column prop="status" label="状态">
            <template #default="scope">
              <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
                {{ scope.row.status === 1 ? '启用' : '禁用' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template #default="scope">
              <el-button type="primary" @click="handleEdit('classType', scope.row)">编辑</el-button>
              <el-button type="danger" @click="handleDelete('classType', scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>

    <!-- 编辑对话框 -->
    <el-dialog
        :title="dialogTitle"
        v-model="dialogVisible"
        width="500px"
    >
      <el-form :model="formData" label-width="100px">
        <el-form-item label="名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
              v-model="formData.status"
              :active-value="1"
              :inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'

const activeTab = ref('category')
const categories = ref([])
const classTypes = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('')
const currentType = ref('')
const formData = ref({
  id: null,
  name: '',
  status: 1
})

// 获取数据
const fetchData = async () => {
  try {
    const [categoriesRes, classTypesRes] = await Promise.all([
      axios.get('/manager-category/categories',{
            headers: { Authorization: localStorage.getItem("token") }
          }),
      axios.get('/class-type/class-types',{
        headers: { Authorization: localStorage.getItem("token") }
      })
    ])
    categories.value = categoriesRes.data.data
    classTypes.value = classTypesRes.data.data
  } catch (error) {
    ElMessage.error('获取数据失败')
  }
}

// 新增
const handleAdd = (type) => {
  currentType.value = type
  dialogTitle.value = type === 'category' ? '新增活动分类' : '新增教学班类型'
  formData.value = {
    id: null,
    name: '',
    status: 1
  }
  dialogVisible.value = true
}

// 编辑
const handleEdit = (type, row) => {
  currentType.value = type
  dialogTitle.value = type === 'category' ? '编辑活动分类' : '编辑教学班类型'
  formData.value = { ...row }
  dialogVisible.value = true
}

// 删除
const handleDelete = async (type, id) => {
  try {
    await ElMessageBox.confirm('确认删除该记录吗？', '提示', {
      type: 'warning'
    })
    const url = type === 'category' ? `/manager-category/category/${id}` : `/class-type/class-type/${id}`
    await axios.delete(url,{
      headers: { Authorization: localStorage.getItem("token") }
    })
    ElMessage.success('删除成功')
    fetchData()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

// 保存
const handleSave = async () => {
  try {
    const url = currentType.value === 'category' ? '/manager-category/category' : '/class-type/class-type'
    await axios.post(url, formData.value,{
      headers: { Authorization: localStorage.getItem("token") }
    })
    ElMessage.success('保存成功')
    dialogVisible.value = false
    fetchData()
  } catch (error) {
    ElMessage.error('保存失败')
  }
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.base-data-manage {
  padding: 20px;
}
.operation-bar {
  margin-bottom: 20px;
}
</style>